package com.coocaa.x.service.litedownloader2.aidl;

import com.coocaa.x.framework.data.JObject;
import com.skyworth.framework.skysdk.util.MD5;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lu on 15-6-17.
 */
public class DownloadTask extends JObject {
    public static class DownloadTaskOnSuccessAction extends JObject {
        public static final String AUTO_INSTALL_ACTION = "OnSuccessAction.other.default.autoinstall";


        public static DownloadTaskOnSuccessAction createAutoInstallAction(String apk_file) {
            DownloadTaskOnSuccessAction action = new DownloadTaskOnSuccessAction();
            action.action_type = ACTION_TYPE.OTHER;
            action.action_data = AUTO_INSTALL_ACTION;
            action.params.put("apk_file", apk_file);
            return action;
        }


        public static DownloadTaskOnSuccessAction create(ACTION_TYPE type, String action_data, String pkg) {
            DownloadTaskOnSuccessAction ret = new DownloadTaskOnSuccessAction();
            ret.action_type = type;
            ret.action_data = action_data;
            ret.pkg = pkg;
            return ret;
        }

        public static enum ACTION_TYPE {
            ACTION, URI, ACTIVITY, OTHER
        }

        public ACTION_TYPE action_type;
        public String action_data = null;

        public String pkg;
        public Map<String, String> params = new HashMap<String, String>();

        public static final Creator<DownloadTaskOnSuccessAction> CREATOR = createCREATOR(DownloadTaskOnSuccessAction.class, null);
    }

    public static final int STATUS_WAITING = 1;
    public static final int STATUS_DOWNLOADING = 2;
    public static final int STATUS_PAUSE = 3;
    public static final int STATUS_SUCCESS = 4;

    public String save_path, download_url;
    public String extra;
    public DownloadTaskOnSuccessAction action = null;

    private String ID = null;

    public DownloadTask() {

    }

    public DownloadTask(String save_path, String download_url) {
        this.save_path = save_path;
        this.download_url = download_url;
    }

    @Override
    public boolean equals(Object o) {
        try {
            DownloadTask task = (DownloadTask) o;
            return task.download_url.equals(download_url) && task.save_path.equals(save_path);
        } catch (Exception e) {
            return false;
        }
    }

    public String _getID() {
        if (ID == null)
            ID = MD5.md5s(toJSONString());
        return ID;
    }

    public static final Creator<DownloadTask> CREATOR = createCREATOR(DownloadTask.class, null);
}
